<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>控制面板</title>
    <style>
        /* 页面背景和字体设置 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
            color: #333;
            background-image: url('./beijing.png');
            background-size: cover;  /* 让图片充满整个页面 */
            background-position: center;  /* 图片居中 */
        }

        /* 页面容器 */
        .container {
            width: 90%;
            margin: 0 auto;
            padding-top: 50px;
        }

        h1 {
            text-align: center;
            color: #5e5e5e;
        }

        /* 导航链接样式 */
        nav {
            text-align: center;
            margin-bottom: 30px;
        }

        nav a {
            margin: 0 15px;
            text-decoration: none;
            color: #007bff;
            font-size: 18px;
            font-weight: bold;
        }

        nav a:hover {
            color: #0056b3;
        }

        /* 控制模块容器 */
        form {
            display: grid;
            grid-template-columns: repeat(4, 1fr); /* 默认每行4个模块 */
            gap: 20px;
            background-color: #ffffff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            opacity: 0.9;
        }

        /* 每个控制开关模块 */
        .data-module {
            background-color: #ffffff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }

        .data-module label {
            font-weight: bold;
            margin-bottom: 10px;
        }

        .switch {
            position: relative;
            display: inline-block;
            width: 60px;
            height: 34px;
        }

        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: 0.4s;
            border-radius: 34px;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 26px;
            width: 26px;
            border-radius: 50%;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: 0.4s;
        }

        input:checked + .slider {
            background-color: #007bff;
        }

        input:checked + .slider:before {
            transform: translateX(26px);
        }

        /* 响应式布局：调整每行显示的模块数量 */
        @media screen and (max-width: 1200px) {
            form {
                grid-template-columns: repeat(3, 1fr); /* 每行3个模块 */
            }
        }

        @media screen and (max-width: 768px) {
            form {
                grid-template-columns: repeat(2, 1fr); /* 每行2个模块 */
            }
        }

        @media screen and (max-width: 480px) {
            form {
                grid-template-columns: 1fr; /* 每行1个模块 */
            }
        }

        /* 提交按钮样式 */
        .submit-btn {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 5px;
            font-size: 18px;
            cursor: pointer;
            margin-top: 20px;
            display: block;
            width: 100%;
            transition: background-color 0.3s ease;
        }

        .submit-btn:hover {
            background-color: #0056b3;
        }
    </style>
    <script type="text/javascript">
        let isUpdating = false; // 标志位，用于控制是否允许刷新数据

        setInterval(function() {
            init();
        }, 3000);

        // 初始化页面
        function init() {
            if (!isUpdating) {
                refresh(); // 只有在不更新数据时才进行刷新
            }
        }

        // 刷新设备状态
        function refresh() {
            var xhr = new XMLHttpRequest();
            var url = ""; // 替换为实际请求的URL

            xhr.open("POST", url, true);
            var post_req = {
                "type": 1,
                "limit": "all"
            };

            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var response = xhr.responseText;
                    var info = JSON.parse(response);
                    // 更新页面上的开关状态
                    info.data.forEach(function(item) {
                        const switchId = getSwitchIdByKey(item.key);
                        if (switchId) {
                            document.getElementById(switchId).checked = item.val === "1";
                        }
                    });
                }
            };

            xhr.send(JSON.stringify(post_req));
        }

        // 根据key值获取对应的开关ID
        function getSwitchIdByKey(key) {
            const switchMap = {
                103: "VehACSwitch",
                105: "light",
                107: "oxygen_switch",
                305: "LEDSwitch",
                306: "AlarmSwitch",
                307: "FanSwitch",
                308: "motor",
                309: "Flame_State",
                310: "button"
            };
            return switchMap[key] || null;
        }

        // 发送开关状态数据
        function sendData(key, value) {
            var xhr = new XMLHttpRequest();
            var url = ""; // 替换为实际请求的URL

            xhr.open("POST", url, true);
            var post_req = {
                "type": 2,
                "data": {
                    "key": key,  // key值对应设备的标识符
                    "val": value  // val值对应开关状态（例如，布尔值，0或1）
                }
            };

            // 控制命令下发前，设置 isUpdating 为 true
            isUpdating = true;

            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var response = xhr.responseText;
                    var info = JSON.parse(response);
                    console.log(info);

                    // 控制命令执行完毕后，更新标志位为 false
                    isUpdating = false;

                    // 刷新数据，确保获取到最新状态
                    refresh();
                }
            };

            xhr.send(JSON.stringify(post_req));
        }

        // 监听开关状态变化
        function setupSwitchListeners() {
            const switches = document.querySelectorAll('.switch input[type="checkbox"]');
            switches.forEach(function(switchElement) {
                switchElement.addEventListener('change', function() {
                    const key = switchElement.id;
                    const value = switchElement.checked ? 1 : 0;
                    sendData(key, value); // 发送控制命令
                });
            });
        }

        window.onload = function() {
            init();
            setupSwitchListeners();
        };
    </script>
</head>
<body>
    <div class="container">
        <h1>基于边缘网关的智能医疗监护系统</h1>
        <nav>
            <a href="temp_stm32.html">控制页面</a>
            <a href="temp_modbus.html">数据显示页面</a>
            <a href="1.html">实时环境监控</a>
        </nav>
        <form>
            <!-- 控制开关模块 -->
            <div class="data-module">
                <label for="VehACSwitch">空调开关</label>
                <label class="switch">
                    <input type="checkbox" id="VehACSwitch" />
                    <span class="slider"></span>
                </label>
            </div>

            <div class="data-module">
                <label for="AlarmSwitch">蜂鸣器开关</label>
                <label class="switch">
                    <input type="checkbox" id="AlarmSwitch" />
                    <span class="slider"></span>
                </label>
            </div>

            <div class="data-module">
                <label for="FanSwitch">通风开关</label>
                <label class="switch">
                    <input type="checkbox" id="FanSwitch" />
                    <span class="slider"></span>
                </label>
            </div>

            <div class="data-module">
                <label for="motor">呼叫按钮</label>
                <label class="switch">
                    <input type="checkbox" id="motor" />
                    <span class="slider"></span>
                </label>
            </div>

            <div class="data-module">
                <label for="Flame_State">消防警报</label>
                <label class="switch">
                    <input type="checkbox" id="Flame_State" />
                    <span class="slider"></span>
                </label>
            </div>

            <div class="data-module">
                <label for="button">提醒事件</label>
                <label class="switch">
                    <input type="checkbox" id="button" />
                    <span class="slider"></span>
                </label>
            </div>

            <div class="data-module">
                <label for="light">室内灯光</label>
                <label class="switch">
                    <input type="checkbox" id="light" />
                    <span class="slider"></span>
                </label>
            </div>

            <div class="data-module">
                <label for="oxygen_switch">供氧开关</label>
                <label class="switch">
                    <input type="checkbox" id="oxygen_switch" />
                    <span class="slider"></span>
                </label>
            </div>

            <div class="data-module">
                <label for="LEDSwitch">LED灯光</label>
                <label class="switch">
                    <input type="checkbox" id="LEDSwitch" />
                    <span class="slider"></span>
                </label>
            </div>

        </form>
    </div>
</body>
</html>
